const express = require('express')
const router = express.Router()
const multiparty = require('multiparty')

//@POST /api/upload/upload_image
router.post('/upload_image',(req, res) => {
  let form = new multiparty.Form({uploadDir:'public/image'});
  // form.uploadDir="image";
  // form.keepExtensions=true;   //是否保留后缀
  form.parse(req,(err,fields,files) =>{  //其中fields表示你提交的表单数据对象，files表示你提交的文件对象
    // console.log(req);
    console.log(fields,files); 
    if(err){
      res.json({
        status:"1",
        msg:err.message
      });
    }else{
      const surl = files.imgFile[0].path

      //此处图片的路径问题还需要处理
      let $url = surl.split('\\')
      let url = 'http://' + req.headers.host + '/' + $url[1] + '/' + $url[2]
      res.json({ 
        status:"0",
        imgSrc: url,
        msg:"上传成功！",
      });
    }
  })  
})

module.exports = router